#!/usr/bin/env python
# -*- coding:utf-8 -*-

import urllib2

URLERROR_CODE = 600

def proxy_auto(opener,request):
    try:
        response = opener.open(request, timeout=2)
    except urllib2.HTTPError,err:
        return err.code
    except urllib2.URLError,err:
        # 这个状态是程序员自定义的，表示URLError
        return URLERROR_CODE
    else:
        # 如果没有异常，则返回响应的状态码
        return response.getcode()

if __name__ == '__main__':
    proxy_list = [
        {"http": "mr_mao_hacker:sffqry9r@120.27.218.32:16816"},
        {"http": "mr_mao_hacker:sffqry9r@120.27.218.32:16816"},
        {"http": "mr_mao_hacker1:sffqry9r@120.27.218.32:168161"},

    ]

    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"}
    reqeust = urllib2.Request("http://www.baidu.com/", headers=headers)

    good_proxy_list = []

    for proxy in proxy_list:
        #构建代理服务器
        proxy_handler = urllib2.ProxyHandler(proxy)
        #构建自定义opener对象
        opener = urllib2.build_opener(proxy_handler)
        #opener对象和请求对象作为参数传递给proxy_auto进行验证
        status_code = proxy_auto(opener,reqeust)

        if status_code == 200:
            good_proxy_list.append(proxy)

    print good_proxy_list









